package com.cookor.poi.modules.mx.util;

import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {
  public static String encode(String str, String pwd) {
    byte[] result = null;
    try {
      KeyGenerator kgen = KeyGenerator.getInstance("AES");
      SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
      random.setSeed(pwd.getBytes());
      kgen.init(128, random);
      SecretKey secretKey = kgen.generateKey();
      byte[] enCodeFormat = secretKey.getEncoded();
      SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      cipher.init(1, key);
      byte[] byteContent = str.getBytes();
      result = cipher.doFinal(byteContent);
    } catch (Exception e) {
      return null;
    } 
    return parseByte2HexStr(result);
  }
  
  public static String decode(String str, String pwd) {
    byte[] result = null;
    byte[] content = parseHexStr2Byte(str);
    try {
      KeyGenerator kgen = KeyGenerator.getInstance("AES");
      SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
      random.setSeed(pwd.getBytes());
      kgen.init(128, random);
      SecretKey secretKey = kgen.generateKey();
      byte[] enCodeFormat = secretKey.getEncoded();
      SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      cipher.init(2, key);
      result = cipher.doFinal(content);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } 
    return new String(result);
  }
  
  private static String parseByte2HexStr(byte[] buf) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < buf.length; i++) {
      String hex = Integer.toHexString(buf[i] & 0xFF);
      if (hex.length() == 1)
        hex = '0' + hex; 
      sb.append(hex.toUpperCase());
    } 
    return sb.toString();
  }
  
  private static byte[] parseHexStr2Byte(String hexStr) {
    if (hexStr.length() < 1)
      return null; 
    byte[] result = new byte[hexStr.length() / 2];
    for (int i = 0; i < hexStr.length() / 2; i++) {
      int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
      int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
      result[i] = (byte)(high * 16 + low);
    } 
    return result;
  }
}
